<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
// require('echarts/lib/component/graphic');
// require('echarts/lib/component/grid');
// require('echarts/lib/component/legend');
// require('echarts/lib/chart/bar');
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.initChart(val)
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      // let xAxisList=data.xAxisList
      // let yAxisDataList=data.yAxisDataList
      const rawData = [
        [100, 302, 301, 334, 390, 330, 320],
        [320, 132, 101, 134, 90, 230, 210],
        [220, 182, 191, 234, 290, 330, 310],
        [150, 212, 201, 154, 190, 330, 410],
        [820, 832, 901, 934, 1290, 1330, 1320]
      ];
      const totalData = [];
      for (let i = 0; i < rawData[0].length; ++i) {
        let sum = 0;
        for (let j = 0; j < rawData.length; ++j) {
          sum += rawData[j][i];
        }
        totalData.push(sum);
      }
      const grid = {
        left: 100,
        right: 100,
        top: 50,
        bottom: 50
      };
      const gridWidth =  this.chart .getWidth() - grid.left - grid.right;
      const gridHeight =  this.chart .getHeight() - grid.top - grid.bottom;
      const categoryWidth = gridWidth / rawData[0].length;
      const barWidth = categoryWidth * 0.6;
      const barPadding = (categoryWidth - barWidth) / 2;
      const series = [
        '石英砂',
        '氟硅酸钠',
        '方解石',
        '碎玻璃',
        '天燃气'
      ].map((name, sid) => {
        return {
          name,
          type: 'bar',
          stack: 'total',
          barWidth: '60%',
          label: {
            show: true,
            formatter: (params) => Math.round(params.value * 1000) / 10 + '%'
          },
          data: rawData[sid].map((d, did) =>
            totalData[did] <= 0 ? 0 : d / totalData[did]
          )
        };
      });
      const color = ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de'];
      const elements = [];
      for (let j = 1, jlen = rawData[0].length; j < jlen; ++j) {
        const leftX = grid.left + categoryWidth * j - barPadding;
        const rightX = leftX + barPadding * 2;
        let leftY = grid.top + gridHeight;
        let rightY = leftY;
        for (let i = 0, len = series.length; i < len; ++i) {
          const points = [];
          const leftBarHeight = (rawData[i][j - 1] / totalData[j - 1]) * gridHeight;
          points.push([leftX, leftY]);
          points.push([leftX, leftY - leftBarHeight]);
          const rightBarHeight = (rawData[i][j] / totalData[j]) * gridHeight;
          points.push([rightX, rightY - rightBarHeight]);
          points.push([rightX, rightY]);
          points.push([leftX, leftY]);
          leftY -= leftBarHeight;
          rightY -= rightBarHeight;
          elements.push({
            type: 'polygon',
            shape: {
              points
            },
            style: {
              fill: color[i],
              opacity: 0.25
            }
          });
        }
      }
      this.chart.setOption({
        legend: {
          selectedMode: false
        },
        grid,
        yAxis: {
          type: 'value'
        },
        xAxis: {
          type: 'category',
          data: ['7月', '8月', '9月', '10月', '11月', '12月', '1月']
        },
        series,
        graphic: {
          elements
        }
      })
    }
  }
}
</script>
